//============================================================
//write by bxl at 2021-06-22 16:08:53
//============================================================


 
using System.Collections.Generic;
using System;
using BCService.Utils;
using BCCommon;
using BCData.CRM.BigCustomerFreightConfigExtention;
using BCDto.CRM.BigCustomerFreightConfigExtention;
using BCEntity.CRM.BigCustomerFreightConfigExtention;
using BCDto.Sim.Admin.Admin;
using BCData.TMS.FreightConfigurationExtention;
using BCService.TMS.VehicleTypeService;
using BCData.TMS.VehicleCategoryReference;
using BCEntity.Common.EntityNameConst;
using BCData.CRM.BigCustomer;
using BCData.TMS.Agent.AgentRecord;
using BCCommon.TMS.Order;
using BCCommon.TMS.FreightEnums;

namespace BCService.CRM.BigCustomerFreightConfigExtention
{
    public class BigCustomerFreightConfigExtentionService : IBigCustomerFreightConfigExtentionService
    {
        #region var
        private readonly IDatabaseContext databaseContext;
        private IBigCustomerFreightConfigExtentionData bigCustomerFreightConfigExtentionData;

        private readonly IFreightConfigurationExtentionData freightConfigurationExtentionData; 
        private readonly IRedisService redisService;
        private readonly IVehicleTypeService vehicleTypeService;
        private readonly IVehicleCategoryReferenceData vehicleCategoryReferenceData;
        private readonly IBigCustomerData bigCustomerData;
        private readonly IAgentRecordData agentRecordData;
        private readonly string key = string.Format("{0}", EntityNameConst.BigCustomerFreightConfigExtentionEntity);

        #endregion

        #region 构造函数 

        public BigCustomerFreightConfigExtentionService(IDatabaseContext databaseContext,
            IBigCustomerFreightConfigExtentionData bigCustomerFreightConfigExtentionData,
            IFreightConfigurationExtentionData freightConfigurationExtentionData,
            IRedisService redisService, IVehicleTypeService vehicleTypeService,
            IVehicleCategoryReferenceData vehicleCategoryReferenceData,
            IBigCustomerData bigCustomerData, 
            IAgentRecordData agentRecordData)
        {
            this.databaseContext = databaseContext;
            this.bigCustomerFreightConfigExtentionData = bigCustomerFreightConfigExtentionData;
            this.freightConfigurationExtentionData = freightConfigurationExtentionData;
            this.redisService = redisService;
            this.vehicleTypeService = vehicleTypeService;
            this.vehicleCategoryReferenceData = vehicleCategoryReferenceData;
            this.bigCustomerData = bigCustomerData;
            this.agentRecordData = agentRecordData;
        }
        #endregion

        #region Add
        public BigCustomerFreightConfigExtentionDto Add(BigCustomerFreightConfigExtentionRequestDto bigCustomerFreightConfigExtentionRequestDto, AdminDto admin)
        {
            var org = bigCustomerFreightConfigExtentionData.GetEntityAsync(bigCustomerFreightConfigExtentionRequestDto.FreightConfigurationExtentionId, bigCustomerFreightConfigExtentionRequestDto.BigCustomerId).GetAwaiter().GetResult();
            if (org != null)
                throw new ArgumentException("要添加的记录重复!");

            var bigCustomerEntity = bigCustomerData.GetEntityAsync(bigCustomerFreightConfigExtentionRequestDto.BigCustomerId).GetAwaiter().GetResult() ?? throw new ArgumentException("大客户信息不存在!");
            var agentEntity = agentRecordData.GetEntityByCompanyId(bigCustomerEntity.CompanyId).GetAwaiter().GetResult() ?? throw new ArgumentException("大客户归属代理商不存在!");
            var freightConfigurationEntity = freightConfigurationExtentionData.GetById(bigCustomerFreightConfigExtentionRequestDto.FreightConfigurationExtentionId).GetAwaiter().GetResult() ?? throw new ArgumentException("运费扩展配置不存在!");

            BigCustomerFreightConfigExtentionEntity entity = bigCustomerFreightConfigExtentionRequestDto.As<BigCustomerFreightConfigExtentionEntity>();

            entity.FreightConfigurationExtentionId = bigCustomerFreightConfigExtentionRequestDto.FreightConfigurationExtentionId;
            entity.CompanyId = bigCustomerEntity.CompanyId;
            entity.AgentRecordId = agentEntity.AgentRecordId;

            entity.CreateAdminId = admin.AdminId;
            entity.CreateAdminName = admin.FullName;
            entity.CreateTime = DateTime.Now;
            return bigCustomerFreightConfigExtentionData.InsertAsync(entity).GetAwaiter().GetResult().As<BigCustomerFreightConfigExtentionDto>();
        }
        #endregion

        #region update
        public BigCustomerFreightConfigExtentionDto Update(BigCustomerFreightConfigExtentionPutDto bigCustomerFreightConfigExtentionPutDto, AdminDto admin)
        {
            var entity = bigCustomerFreightConfigExtentionData.GetEntityAsync(bigCustomerFreightConfigExtentionPutDto.BigCustomerFreightConfigExtentionId).GetAwaiter().GetResult()?? throw new ArgumentException("要修改的记录不存在!");

            entity.FirstWeight = bigCustomerFreightConfigExtentionPutDto.FirstWeight;
            entity.MaxWeight = bigCustomerFreightConfigExtentionPutDto.MaxWeight;
            entity.StartPrice = bigCustomerFreightConfigExtentionPutDto.StartPrice;
            entity.EveryWeightPrice = bigCustomerFreightConfigExtentionPutDto.EveryWeightPrice;
            entity.MileagePrice = bigCustomerFreightConfigExtentionPutDto.MileagePrice;

            entity.SameCountyMaxMileage = bigCustomerFreightConfigExtentionPutDto.SameCountyMaxMileage;
            entity.SameCountyRatio = bigCustomerFreightConfigExtentionPutDto.SameCountyRatio;
            entity.SameCityMaxMileage = bigCustomerFreightConfigExtentionPutDto.SameCityMaxMileage;
            entity.SameCityRatio = bigCustomerFreightConfigExtentionPutDto.SameCityRatio;
            entity.SameProvinceMileage = bigCustomerFreightConfigExtentionPutDto.SameProvinceMileage;
            entity.SameProvinceRatio = bigCustomerFreightConfigExtentionPutDto.SameProvinceRatio;

            entity.EditAdminId = admin.AdminId;
            entity.EditAdminName = admin.FullName;
            entity.UpdateTime = DateTime.Now;

            this.redisService.DeleteMultipleKey(key, false);
            return bigCustomerFreightConfigExtentionData.UpdateAsync(entity).GetAwaiter().GetResult().As<BigCustomerFreightConfigExtentionDto>();

        }
        #endregion

        #region Delete
        public void Delete(long bigCustomerFreightConfigExtentionId)
        {
            this.redisService.DeleteMultipleKey(key, false);
            bigCustomerFreightConfigExtentionData.DeleteAsync(bigCustomerFreightConfigExtentionId).GetAwaiter().GetResult();

        }
        #endregion

        #region GetByBigCustomerFreightConfigExtentionId
        public BigCustomerFreightConfigExtentionDto GetById(long bigCustomerFreightConfigExtentionId)
        {
            string cacheKey = this.redisService.GetKey(key, bigCustomerFreightConfigExtentionId);


            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var a = bigCustomerFreightConfigExtentionData.GetEntityAsync(bigCustomerFreightConfigExtentionId).GetAwaiter().GetResult();
                return a.As<BigCustomerFreightConfigExtentionDto>();
            }
            , TimeSpan.FromDays(1));
            return result;
        }
        #endregion

        #region GetList  获取大客户运费配置扩展记录--[分页]
        public PageableList<BigCustomerFreightConfigExtentionViewDto> GetList(AdminDto admin, long? bigCustomerId, int? vehicleTypeId, int? vehicleCategoryReferenceId, string provinceCode, string cityCode, string countyCode, OrderSourceType? sourceType, FreightType? freightType, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key,admin.CompanyId, bigCustomerId, vehicleTypeId, vehicleCategoryReferenceId, provinceCode, cityCode, countyCode, sourceType, freightType, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                int fCount = 0;
            IEnumerable<BigCustomerFreightConfigExtentionEntity> fList;

            (fList, fCount) = bigCustomerFreightConfigExtentionData.GetListAsync(admin.CompanyId, null, bigCustomerId, vehicleTypeId, vehicleCategoryReferenceId, provinceCode, cityCode, countyCode, sourceType, freightType, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<BigCustomerFreightConfigExtentionViewDto>()
            {
                Count = fCount,
                Items = fList.As<IEnumerable<BigCustomerFreightConfigExtentionViewDto>>(),
                PageIndex = pageIndex,
                PageSize = pageSize,
                ServerTime = DateTime.Now,
            };
            }, TimeSpan.FromDays(30));
            return result;
        }
        #endregion



        public BigCustomerFreightConfigExtentionDto GetByFreightExtentionId(long freightConfigurationExtentionId, long bigCustomerId)
        {
            string cacheKey = this.redisService.GetKey(key+":FreightExtention", freightConfigurationExtentionId, bigCustomerId);

            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var a = bigCustomerFreightConfigExtentionData.GetEntityAsync(freightConfigurationExtentionId, bigCustomerId).GetAwaiter().GetResult();
                return a.As<BigCustomerFreightConfigExtentionDto>();
            }
            , TimeSpan.FromDays(1));
            return result;
        }
    }
}